package com.kly.api.ai;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.kly.api.ai.model.CreateImageOutPaintingResponse;
import com.kly.api.ai.model.GetImageOutPaintingResponse;
import com.kly.api.ai.model.ImageOutPaintingRequest;
import com.kly.exception.ErrorCode;
import com.kly.exception.ThrowUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author admin
 * @date 2025/3/15
 */
@Slf4j
@Component
public class AliYunAiApi {

    /**
     * apiKey
     */
    @Value("${aliYun.ai.apiKey}")
    private String apiKey;

    /**
     * 创建任务url
     */
    private static final String CREATE_BASE_URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/out-painting";
    /**
     * 获取任务状态url
     */
    private static final String Get_BASE_URL = "https://dashscope.aliyuncs.com/api/v1/tasks/";

    /**
     * 创建任务请求
     *
     * @param imageOutPaintingRequest
     * @return
     */
    public CreateImageOutPaintingResponse createImageOutPainting(ImageOutPaintingRequest imageOutPaintingRequest) {
        ThrowUtils.throwIf(imageOutPaintingRequest == null, ErrorCode.PARAMS_ERROR, "参数错误");
        HttpRequest httpRequest = HttpRequest.post(CREATE_BASE_URL)
                .header("Authorization", "Bearer " + apiKey)
                .header("X-DashScope-Async", "enable")
                .header("Content-Type", "application/json")
                .body(JSONUtil.toJsonStr(imageOutPaintingRequest))
                .timeout(5000);
        try (HttpResponse response = httpRequest.execute()) {
            ThrowUtils.throwIf(!response.isOk(), ErrorCode.PARAMS_ERROR, "创建任务失败");
            CreateImageOutPaintingResponse createImageOutPaintingResponse = JSONUtil.toBean(response.body(), CreateImageOutPaintingResponse.class);
            String code = createImageOutPaintingResponse.getCode();
            ThrowUtils.throwIf(StrUtil.isNotBlank(code), ErrorCode.PARAMS_ERROR, "创建任务失败");
            return createImageOutPaintingResponse;
        }
    }


    /**
     * 查询任务状态
     */
    public GetImageOutPaintingResponse getImageOutPainting(String taskId) {
    //     curl -X GET \
        // --header "Authorization: Bearer $DASHSCOPE_API_KEY" \
        // https://dashscope.aliyuncs.com/api/v1/tasks/86ecf553-d340-4e21-xxxxxxxxx
        HttpRequest httpRequest = HttpRequest.get(Get_BASE_URL + taskId)
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .timeout(5000);
        try (HttpResponse response = httpRequest.execute()) {
            ThrowUtils.throwIf(!response.isOk(), ErrorCode.PARAMS_ERROR, "查询任务失败");
            GetImageOutPaintingResponse getImageOutPaintingResponse = JSONUtil.toBean(response.body(), GetImageOutPaintingResponse.class);
            return getImageOutPaintingResponse;
        }
    }
}
